home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++
- Subject: Re: Q; Has anyone ever attempted to implement persistent objects?
- Date: 6 Feb 1996 21:37:55 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4f8hnj$g02@dawn.mmm.com>
- References: <4f688s$ctc@news.cc.geneseo.edu>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Peter Denecke (prd97) wrote:
- > I was wondering if anyone ever attempted to implement a persistent objects
- > system, that is, objects that can exist beyond run time. If so, could you
- > give
- > an overview of your implementation and describe the problems you faced in
- > developing it?
-
- Any object can be "trained" to dump itself to a file and read itself back in.
- The trouble is that in order for an object to read itself back in, there needs
- to actually be an object. The difficult part about persistence is in getting
- the same class instantiated that was written out in the first place.
-
- There are many 3rd party class libraries out there that have implemented
- persistence, with handy features of varying degree. If persistence is
- your goal, rather than your own implementation of persistence, you might
- reach it faster if you shopped a bit for some libraries. I think there
- are even some free ones out there (library vendors, speak up now).
-
- I implemented a cheap form of it for our use internally. We needed not only
- to store objects on disk, but to transfer them between processes via sockets.
- It turns out that the same solution works for both problems. In a nutshell,
- we invented a base class (call it "Persistent") with virtual functions:
- class Persistent
- {
- public:
- ...
- virtual void store(ostream& os) const; // writes self to os
- virtual void load(istream& is); // restores self from os
- ...
- };
-
- Besides this, there is a separate entity which stores and retrieves these
- Persistent objects:
- class Storage
- {
- public:
- ...
- void store(const Persistent& obj, ostream &os);
- Persistent* load(istream& is);
- ...
- };
-
- Conceptually, these work like this:
- void Storage::store(const Persistent& obj, ostream &os)
- {
- ClassInfo info(obj.classInfo());
- os << info;
- obj.store(os);
- }
- Persistent* Storage::load(istream& is)
- {
- ClassInfo info;
- is >> info;
- Persistent* obj = newPersistent(info);
- obj->load(is);
- return obj;
- }
-
- The "hole" in this example is the function lookup(). That could be as simple
- as a table that associates class names with "factory" Persistent objects that
- construct empty Persistent objects of their same class.
-
- One of the problems I countered was how to make a library of these objects
- that is extensible. Virtual functions go almost all the way there, but how
- can a user add a class and still have the existing library function lookup()
- find it? Basically there needs to be some kind of "registration" mechanism
- that allows the insertion of factory objects into the table that lookup()
- uses. I used a special constructor for derivatives of Persistent. The
- convention is that exactly one object of a given class will be constructed
- with the registration constructor, getting it inserted into the lookup()
- table.
-
- If you're going to be persistent across different machine architectures,
- you also need to think about data formats. If you write things in binary
- format, integer byte order or floating point formats can vary. You might
- choose to use text format for storing and retrieving data. It is more
- portable, but is slower and results in larger data stores.
-
- The load and store functions must be kept in sync. If you add data
- members to a Persistent object, you need to update both load and store
- functions in the same way. Another problem related to this is the
- versioning of data. If you store some Persisent object, then update
- the class, you can no longer read it back in without some extra code
- to manage changes of this kind. You have to decide whether this is
- a problem. If it is, you need to think about whether to build versioning
- into the classes, or just invent new classes.
-
- I know this is just a sketch, but I hope it helps.
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-